/*
 Various simple javascript tools for standart *PickerList.jsp, *List.jsp and *Edit.jsp
 */

/* Generate GUID compilant to rfc4122 v4 */
function generateGUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
}

// Get correct ajax object
function getXmlHttpObject() {
    if (typeof(XMLHttpRequest) === undefined) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP.6.0");
        } catch(e) {}
        try {
            return new ActiveXObject("Msxml2.XMLHTTP.3.0");
        } catch(e) {}
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {}
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
             return null;
        }
    } else {
        return new XMLHttpRequest();
    }
}

// Compatibility for IE up to version 8
function getElementsByNameFix(tag, name) {
    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for(i = 0, iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");
        if(att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}

// ename - entity name
// id    - selected id
// dname - name of selected id
function selectTableRow(ename, id) {
    var rows = document.getElementsByName('tableItemOdd');
    if (!rows.length) {
        rows = getElementsByNameFix('tr', 'tableItemOdd');
    }
    for(var i=0; i<rows.length; i++) {
        var value = rows[i];
        value.className='rowOdd';
    }
    rows = document.getElementsByName('tableItemEven');
    if (!rows.length) {
        rows = getElementsByNameFix('tr', 'tableItemEven');
    }
    for(var i=0; i<rows.length; i++) {
        var value = rows[i];
        value.className='rowEven';
    }

    document.getElementById(id).className='rowSelect';

    var xmlHttp = getXmlHttpObject();
    if (xmlHttp == null) {
        alert ('Your browser does not support Ajax HTTP');
        return;
    }

    // Store the hidden picker id in session
    xmlHttp.open("GET", "SetPickerIdVariable.jsp?e=" + ename + "&i=" + id + "&guid=" + generateGUID(), true);
    xmlHttp.send(null);
}

function toggleVisibility(id) {
    var e=document.getElementById(id)
    if(!e) {
        return
    }
    if (e.style.display=="none") {
        e.style.display="table"
    } else {
        e.style.display="none"
    }
}

function toggleVisibility(id,icon) {
    var e=document.getElementById(id)
    var i=document.getElementById(icon)
    if(!e || !i) {
        return
    }
    if (e.style.display=="none") {
        e.style.display="block"
        i.className="wrapperArrowUp"
    } else {
        e.style.display="none"
        i.className="wrapperArrowDown"
    }
}
